#include "mainwindow.h"
#include "QRoleReader.h"
#include "ui_mainwindow.h"
#include "Variate.h"
#include <QFile>
#include "QRoleReader.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->passwordLineEdit, SIGNAL(returnPressed()), this, SLOT(on_logInButton_clicked()));
    db = DBOperation::getInstance();
    db->open();

    this->setStyleSheet("QWidget{"
                        "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(251,102,102, 200), stop:1 rgba(20,196,188, 210));"
                        "}");
    ui->passwordLineEdit->setEchoMode(QLineEdit::Password);

    ui->background->setStyleSheet("QPushButton{background-color: rgb(255,255,255);"
                                  "border: none;border-radius:35px;}"
                                  "QPushButton:hover{background-color: rgb(255,255,255);}"//hover
                                  "QPushButton:pressed{background-color: rgb(255,255,255);}");

    ui->usernameLineEdit->setPlaceholderText("请输入账号");
    ui->usernameLineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->passwordLineEdit->setPlaceholderText("请输入密码");
    ui->passwordLineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->comboBox->setStyleSheet("QComboBox{border-radius: 15px;"
                                "padding: 1px 10px 1px 5px;"
                                "border: 2px solid rgb(20,196,188);"
                                "font: 25 20pt '微软雅黑 Light';}"
                                "QListView::item:hover {color: rgb(255,255,255);background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgb(17,171,164), stop:1 rgb(17,171,164));}"
                                "QListView::item:selected {color: rgb(255,255,255);background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgb(22,218,208), stop:1 rgb(22,218,208));}"
                                "QComboBox::drop-down {"
                                "subcontrol-origin:paddind;  subcontrol-position: top right;width: 15px;"
                                "border-left-width: 1px;border-left-color: darkgray;border-left-style: solid;border-top-right-radius: 3px;border-bottom-right-radius: 3px;}");

    ui->logInButton->setStyleSheet("QPushButton{font: 25 20pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(20,196,188);"
                                   "border: none;border-radius:15px;}"
                                   "QPushButton:hover{background-color: rgb(22,218,208);}"//hover
                                   "QPushButton:pressed{background-color: rgb(17,171,164);}");

    ui->registerButton->setStyleSheet("QPushButton{font: 25 16pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(20,196,188);"
                                      "border: none;border-radius:15px;}"
                                      "QPushButton:hover{background-color: rgb(22,218,208);}"//hover
                                      "QPushButton:pressed{background-color: rgb(17,171,164);}");


    ui->whiteWidget->setStyleSheet("background-color:white;");
}

MainWindow::~MainWindow()
{
    db->close();
    delete ui;
}

void widgetShow(void* obj){
    MainWindow *w = (MainWindow*)obj;
    w->show();
}

void MainWindow::on_logInButton_clicked()
{
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    QString identity = ui->comboBox->currentText();
    QString sql;

    /*
    QSqlTableModel* model = new QSqlTableModel(nullptr, DBOperation::getDatabase());
    model->setTable("reader_info");
    model->setFilter(QString("readerNumber='%0'").arg(username));
    model->select();
    */

    if (identity == "管理员") {
        sql = "select * from admin;";
    }
    else {
        sql = "select * from reader_info;";
    }
    db->operate(sql);
    bool flag = false;
    while (db->getNext()) {
        if (db->getValue("username") == username) {
            flag = true;
            if (db->getValue("password") == password) {
                if (identity == "管理员") {
                    admin_username = username;
                    QMessageBox MyBox(QMessageBox::Information, "提示", "您已获得管理员权限");
                    MyBox.exec();
                    //                    QMessageBox::information(this, "提示", "您已获得管理员权限");
                    adm = new AdminCenter(widgetShow, this);
                    this->hide();
                    adm->show();//登录成功，进入管理员窗口（未开发）
                    adm->setWindowTitle("管理员中心");
                }
                else {
                    QRoleReader* role = QRoleReader::getInstance();
                    role->setUserName(username);
                    role->setPassword(password);
                    role->setReader(db->getValue("readerNumber").toString());

                    reader_username = username;
                    QMessageBox MyBox(QMessageBox::Information, "提示", "您以读者身份进入");
                    MyBox.exec();
                    rea = new ReaderCenter(widgetShow, this);
                    this->close();
                    rea->show();//登录成功，进入读者窗口（未开发）
                    rea->setWindowTitle("读者中心");
                }
            }
            else {
                QMessageBox MyBox(QMessageBox::Warning, "提示", "密码错误");
                MyBox.exec();
            }
            break;
        }
    }
    if (!flag) {
        QMessageBox MyBox(QMessageBox::Warning, "警告", "该用户名不存在");
        MyBox.exec();
    }
}


void MainWindow::on_registerButton_clicked()
{
    reg = new registerWindow(widgetShow, this);
    this->hide();
    reg->setWindowTitle("读者注册");
    reg->show();
}
